#include "tokensettings.h"
#include "TokenColors.h"
#include <SimpleIni.h>
#include <absl/strings/match.h>
#include <absl/strings/str_split.h>
#define __EXCLUDE_KEYWORDS__ "__EXCLUDE_KEYWORDS__"

TokenSettings::TokenSettings()
{
}

TokenSettings *TokenSettings::shareInstance()
{
  static TokenSettings ts;
  return &ts;
}

bool TokenSettings::load(const std::string &inifile)
{
    /* 1.载入关键字高亮配置 */
    tokenInfos.clear();
    //QSettings s(inifile, QSettings::IniFormat);
    CSimpleIniA s;
    s.LoadFile(inifile.c_str());

    CSimpleIniA::TNamesDepend keys;
    s.GetAllKeys("",keys);
    for(const auto& item : keys){
        if(absl::StartsWith(item.pItem, __EXCLUDE_KEYWORDS__))
            continue;
        const std::string& value = s.GetValue("", item.pItem, "");
        tokenInfos[item.pItem] = enumToInt(colorsMap.at(value));
    }

    /* 载入exclude列表 */
    const std::string& excludes = s.GetValue("", __EXCLUDE_KEYWORDS__, "");
    if(excludes.size()>0){
        excludeList = absl::StrSplit(excludes, "*");
    }
    return true;
}



int TokenSettings::getTokenInfo(const std::string &token)
{
    if(tokenInfos.find(token)!=tokenInfos.end()){
        return tokenInfos[token];
    }else{
        return -1;
    }
}

std::map<std::string, int> &TokenSettings::getTokenInfos()
{
    return tokenInfos;
}

const std::vector<std::string> &TokenSettings::getExcludeList()
{
    return excludeList;
}
